﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using CWB.Common.Constant;
using CWB.Common.DTO;
using CWB.Common.Logging;
using CWB.Web.Configuration;
using CWB.Web.DB;
using CWB.Web.Models;
using CWB.Web.Models.Configuration;
using CWB.Web.MVC.BaseController;
using CWB.WebSite.Models.Bou;
using CWB.Common.Utilities;
using CWB.Web.MVC.Filters;

namespace CWB.WebSite.Controllers.Service
{
    [PermissionFilterAttvibute(loginType = LoginType.Bo)]
    public class BouServiceController : MultiCultureBaseController
    {
        private static readonly ISysLog Log = SysLog.GetLogger(typeof(BouServiceController));

        public BouServiceController(LanguageInfo languageInfo)
            : base(languageInfo)
        {
            ControllerPermision = Permission.Private;
        }

        //
        // GET: /BouService/
        [HttpPost]
        public JsonResult Create(string username, string password)
        {
            DbTableBbu o = new DbTableBbu();
            ResultBase result = new ResultBase();
            if (o.CheckUserNameIsExist(username))
            {
                result.Message = Localization.TrandlateMessage(Err.UserNameIsExisting);
            }
            else
            {
                if (o.createBbu(username, password))
                {
                    result.Message = Localization.TrandlateMessage(Msg.CreateBackOfficeUserSuccess);
                    result.Result = ReturnCode.Success;
                    Log.Debug("Create backoffice user success, username : {0}", username);
                }
                else
                {
                    result.Message = Localization.TrandlateMessage(Err.SaveOrCreateDataGoProblem);
                    Log.Debug("Create backoffice user failed, username : {0}", username);
                }
            }
            return Json(result, JsonRequestBehavior.DenyGet);
        }

        [HttpPost]
        public JsonResult Update(int sn, string password)
        {
            DbTableBbu o = new DbTableBbu();
            ResultBase result = new ResultBase();
            //check the user is exist.
            DbModuleBbu bbu = o.getBbuBySn(sn);
            if (bbu.sn > 0)
            {
                if (o.updateBbuBySN(sn, password))
                {
                    result.Result = ReturnCode.Success;
                    result.Message = Localization.TrandlateMessage(Msg.UpdateBackOfficeUserSuccess);
                }
                else
                {
                    result.Message = Localization.TrandlateMessage(Err.SaveOrCreateDataGoProblem);
                }
            }
            else
            {
                result.Message = Localization.TrandlateMessage(Err.TheBackOfficeUserIsNotExist);
            }
            return Json(result, JsonRequestBehavior.DenyGet);
        }

        [HttpPost]
        public JsonResult Delete(int sn)
        {
            DbTableBbu o = new DbTableBbu();
            ResultBase result = new ResultBase();
            //check the user is exist.
            if (o.DeleteBbuBySn(sn))
            {
                result.Result = ReturnCode.Success;
                result.Message = Localization.TrandlateMessage(Msg.DeleteSuccess);
            }
            else
            {
                result.Message = Localization.TrandlateMessage(Err.DeleteDataFailed);
            }
            return Json(result, JsonRequestBehavior.DenyGet);
        }

        [HttpPost]
        public JsonResult Search(int? page, string searchkey)
        {
            int iPage = 1;
            string sSearchKey = "";
            if (page.HasValue)
            {
                iPage = int.Parse(page.Value.ToString());
            }
            if (!string.IsNullOrEmpty(searchkey))
            {
                sSearchKey = searchkey;
            }
            DbTableBbu odb = new DbTableBbu();
            Paging pg = new Paging()
            {
                pageSize = AppConfigManager.SystemSetting.UsersPageingSize,
                currentPage = iPage
            };
            DBSortingPara dbsp = new DBSortingPara();
            KeyValuePair<string, string> cond = new KeyValuePair<string, string>("username", sSearchKey);
            dbsp.AddSortingItem("username", SortingType.ASC);

            BouListing bl = new BouListing();
            try
            {
                List<DbModuleBbu> listbbu = odb.getAllBbu(ref pg, cond, dbsp);
                if (listbbu.Count > 0)
                {
                    bl.Result = ReturnCode.Success;
                    bl.listbl = new List<BouListingModel>();
                    bl.totalPages = pg.maxPage;
                    foreach (DbModuleBbu dmb in listbbu)
                    {
                        bl.listbl.Add(new BouListingModel() { sn = dmb.sn, UserName = dmb.username });
                    }
                }
                else
                {
                    bl.Message = Localization.TrandlateMessage(Msg.ThereAreNoDateFound);
                    bl.Result = ReturnCode.SuccessWithNoData;
                }
            }
            catch (Exception ex)
            {
                bl.setException(ex, "BouService_Search");
            }
            return Json(bl, JsonRequestBehavior.DenyGet);
        }
    }
}
